﻿using System.Collections.Generic;
using com.guojin.mj.net;
using com.guojin.mj.net.message.login;
using UnityEngine;
using UnityEngine.UI;
using com.guojin.core.io.message;

internal enum ToggleType
{
    EmpowerTakeIn,
    AutoCompute,
    VieForZhuang,
    MaxPlayerCount,
    SameNiu,
    OneTimesBet,        
    ClubRoom,
    LianMeng           
}

internal enum TextType
{
    EmpowerTakeIn,
    AutoCompute,
    VieForZhuang,
    MaxPlayerCount,
    SameNiu,
    OneTimesBet,
    ClubRoom,
    LianMeng,

    RoomNameTex,
    BotScoreTex,
    TakeInScore,
    PropertyTex,                                  //总资产
    ServiceCharge,
}

public class CreateRoomPage : EventManager
{
    private static CreateRoomPage instance;

    public static CreateRoomPage GetInstance()
    {
        return instance;
    }

    void Awake()
    {
        instance = this;
    }

    public  string ClubOrLianMengID;

    public Toggle[] toggleArr;
    public Text[] textArr;
    public Button roomSetBtn;
    public Button clubSetBtn;
    public Button explainBtn;
    public GameObject roomSetPage;
    public GameObject clubSetPage;
    public Slider scoreSlider;
    public Slider timeSlider;
    public GameObject timeSliderGroup;
    public Button createRoombtn;
    public Button closeBtn;

    public override void InformationSetting()
    {
        AddListenerToButton(roomSetBtn, RoomSetBtnClick);
        AddListenerToButton(clubSetBtn, ClubSetBtnClick);
        AddListenerToButton(explainBtn, ExplainBtnClick);
        AddListenerToButton(createRoombtn, CreateRoomBtnClick);
        AddListenerToButton(closeBtn, CloseBtnClick);
        SetText(textArr[11], GameData.GetInstance().gold.ToString());
        ClubToggleInit();
        LianMengToggleInit();
        SetTextSelect(roomSetBtn.transform.FindChild("Text").GetComponent<Text>());

        CreateRoomController.GetInstance().RoomName = "";
        CreateRoomController.GetInstance().EmpowerTakeIn = "false";
        CreateRoomController.GetInstance().AutoCompute = "false";
        CreateRoomController.GetInstance().VieForZhuang = "false";
        CreateRoomController.GetInstance().mingpai = "false";
        CreateRoomController.GetInstance().MaxPlayerCount = "6";
        CreateRoomController.GetInstance().SameNiu = "0";
        CreateRoomController.GetInstance().OneTimesBet = "0";
        CreateRoomController.GetInstance().CreateRoomType = "0";
        CreateRoomController.GetInstance().BotScore = "2";
        CreateRoomController.GetInstance().MinTakeInScore = "200";
        CreateRoomController.GetInstance().MaxTakeInScore = "4";
        CreateRoomController.GetInstance().ServiceCharge = "40";
        CreateRoomController.GetInstance().RoomExistenceTime = "30";
        CreateRoomController.GetInstance().ClubOrLianMengID = "0";
}

    public void SetRoomNameEnd()
    {
        CreateRoomController.GetInstance().RoomName = textArr[(int) TextType.RoomNameTex].text;
    }

    public void RoomSetBtnClick()
    {
        roomSetPage.gameObject.SetActive(true);
        SetTextSelect(roomSetBtn.transform.FindChild("Text").GetComponent<Text>());
        clubSetPage.gameObject.SetActive(false);
        SetTextCancelSelect(clubSetBtn.transform.FindChild("Text").GetComponent<Text>());
    }

    public void ClubSetBtnClick()
    {
        roomSetPage.gameObject.SetActive(false);
        SetTextCancelSelect(roomSetBtn.transform.FindChild("Text").GetComponent<Text>());
        clubSetPage.gameObject.SetActive(true);
        SetTextSelect(clubSetBtn.transform.FindChild("Text").GetComponent<Text>());
    }

    //俱乐部 联盟面板初始化
    public void ClubToggleInit()
    {
        if (GameData.GetInstance().level > 0)
        {
            toggleArr[(int) TextType.ClubRoom].transform.FindChild("fobid").gameObject.SetActive(false);
            toggleArr[(int) TextType.ClubRoom].transform.FindChild("Background").gameObject.SetActive(true);
            toggleArr[(int) TextType.ClubRoom].transform.GetComponent<Toggle>().enabled = true;
        }
        else
        {
            toggleArr[(int) TextType.ClubRoom].transform.FindChild("fobid").gameObject.SetActive(true);
            toggleArr[(int)TextType.ClubRoom].transform.FindChild("Background").gameObject.SetActive(false);
            toggleArr[(int) TextType.ClubRoom].transform.GetComponent<Toggle>().enabled = false;
        }
    }

    public void LianMengToggleInit()
    {
        if (pitchClubRoom && GameData.GetInstance().level == 2)
        {
            toggleArr[(int) TextType.LianMeng].transform.FindChild("fobid").gameObject.SetActive(false);
            toggleArr[(int)TextType.LianMeng].transform.FindChild("Background").gameObject.SetActive(true);
            toggleArr[(int) TextType.LianMeng].transform.GetComponent<Toggle>().enabled = true;
        }
        else
        {
            toggleArr[(int) TextType.LianMeng].transform.FindChild("fobid").gameObject.SetActive(true);
            toggleArr[(int)TextType.LianMeng].transform.FindChild("Background").gameObject.SetActive(false);
            toggleArr[(int) TextType.LianMeng].transform.GetComponent<Toggle>().enabled = false;
        }
    }

    public void ExplainBtnClick()
    {
        UIManager.ChangeUI(UIManager.PageState.CreateRoomExplainPage, (GameObject obj) =>
        {
            obj.GetComponent<CreateRoomExplainPage>().InformationSetting();
        });
    }

    //toggle点击事件
    public void SetToggleIsOn(int toggleNum)                 
    {
        toggleArr[toggleNum].GetComponent<Toggle>().isOn = true;
        SetTextSelect(toggleArr[toggleNum].transform.FindChild("Label").GetComponent<Text>());
    }

    public void SetToggleIsOff(int toggleNum)                   
    {
        toggleArr[toggleNum].GetComponent<Toggle>().isOn = false;
        SetTextCancelSelect(toggleArr[toggleNum].transform.FindChild("Label").GetComponent<Text>());
    }

    public void EmpowerTakeInToggleClick()
    {
        if (toggleArr[(int) ToggleType.EmpowerTakeIn].isOn)
        {
            CreateRoomController.GetInstance().EmpowerTakeIn = "true";
            empowerTakeIn = 100;
            Debug.Log(takeScoreServiceCharge);
            GetAndSetServiceCharge();
            SetTextSelect(toggleArr[(int) ToggleType.EmpowerTakeIn].transform.FindChild("Label").GetComponent<Text>());
        }
        else
        {
            CreateRoomController.GetInstance().EmpowerTakeIn = "false";
            empowerTakeIn = 0;
            Debug.Log(takeScoreServiceCharge);
            GetAndSetServiceCharge();
            SetTextCancelSelect(toggleArr[(int)ToggleType.EmpowerTakeIn].transform.FindChild("Label").GetComponent<Text>());
        }    
    }

    public void AutoComputeToggleClick()
    {
        if (toggleArr[(int) ToggleType.AutoCompute].isOn)
        {
            CreateRoomController.GetInstance().AutoCompute = "true";
            autoCompute = 100;
            GetAndSetServiceCharge();
            SetTextSelect(toggleArr[(int) ToggleType.AutoCompute].transform.FindChild("Label").GetComponent<Text>());
        }
        else
        {
            CreateRoomController.GetInstance().AutoCompute = "false";
            autoCompute = 0;
            GetAndSetServiceCharge();
            SetTextCancelSelect(toggleArr[(int)ToggleType.AutoCompute].transform.FindChild("Label").GetComponent<Text>());
        }
    }

    public void VieForZhuangToggleClick()
    {
        if (toggleArr[(int) ToggleType.VieForZhuang].isOn)
        {
            CreateRoomController.GetInstance().VieForZhuang = "true";
            vieforZhuang = 100;
            GetAndSetServiceCharge();
            SetTextSelect(toggleArr[(int) ToggleType.VieForZhuang].transform.FindChild("Label").GetComponent<Text>());
        }
        else
        {
            CreateRoomController.GetInstance().VieForZhuang = "false";
            vieforZhuang = 0;
            GetAndSetServiceCharge();
            SetTextCancelSelect(toggleArr[(int)ToggleType.VieForZhuang].transform.FindChild("Label").GetComponent<Text>());
        }
    }
    public void MiangPaiMoShi()
    {
        if (toggleArr[8].isOn)
        {
            CreateRoomController.GetInstance().mingpai = "true";
        }
        else
        {
            CreateRoomController.GetInstance().mingpai = "false";
        }
        Debug.Log("CreateRoomController.GetInstance().mingpai"+ CreateRoomController.GetInstance().mingpai);
    }
    public void MaxPlayerCountToggleClick()               //0-6
    { 
        if (toggleArr[(int) ToggleType.MaxPlayerCount].isOn)
        {
            UIManager.ChangeUI(UIManager.PageState.CreateRoomPlayerCountPage, (GameObject obj) =>
            {
                obj.GetComponent<CreateRoomPlayerCountPage>().InformationSetting();
            });
            SetTextSelect(toggleArr[(int)ToggleType.MaxPlayerCount].transform.FindChild("Label").GetComponent<Text>());
        }       
        else
        {
            toggleArr[(int)ToggleType.MaxPlayerCount].isOn = false;
            SetTextCancelSelect(toggleArr[(int)ToggleType.MaxPlayerCount].transform.FindChild("Label").GetComponent<Text>());
            CreateRoomController.GetInstance().MaxPlayerCount = "6";
        }    
    }

    public void SameNiuToggleClick()                           //0 比花色  1平局  2庄家赢
    {
        if (toggleArr[(int)ToggleType.SameNiu].isOn)
        {
            UIManager.ChangeUI(UIManager.PageState.CreateRoomSameNiuPage, (GameObject obj) =>
            {
                obj.GetComponent<CreateRoomSameNiuPage>().InformationSetting();
            });
            SetTextSelect(toggleArr[(int)ToggleType.SameNiu].transform.FindChild("Label").GetComponent<Text>());
        }
        else
        {
            toggleArr[(int)ToggleType.SameNiu].isOn = false;
            SetTextCancelSelect(toggleArr[(int)ToggleType.SameNiu].transform.FindChild("Label").GetComponent<Text>());
            CreateRoomController.GetInstance().SameNiu = "0";
        }
    }

    public void OneTimesBetToggleClick()                   //0 10 15 20
    {
        if (toggleArr[(int) ToggleType.OneTimesBet].isOn)
        {
            UIManager.ChangeUI(UIManager.PageState.CreateRoomOneTimesBetPage, (GameObject obj) =>
            {
                obj.GetComponent<CreateRoomOneTimesBetPage>().InformationSetting();
            });
            SetTextSelect(toggleArr[(int)ToggleType.OneTimesBet].transform.FindChild("Label").GetComponent<Text>());
        }
        else
        {
            toggleArr[(int)ToggleType.OneTimesBet].isOn = false;
            SetTextCancelSelect(toggleArr[(int)ToggleType.OneTimesBet].transform.FindChild("Label").GetComponent<Text>());
            CreateRoomController.GetInstance().OneTimesBet= "0";
            oneTimesBet = 0;
            GetAndSetServiceCharge();
        }
    }

    private bool pitchClubRoom = false;
    public void ClubRoomToggleClick()
    {
        if (toggleArr[(int) ToggleType.ClubRoom].isOn)
        {
            if (CreateRoomController.GetInstance().CreateRoomType == "0")
            {
                CreateRoomController.GetInstance().CreateRoomType = "1";
            }         
            SetTextSelect(toggleArr[(int)ToggleType.ClubRoom].transform.FindChild("Label").GetComponent<Text>());
            pitchClubRoom = true;
            LianMengToggleInit();
        }
        else
        {
            CreateRoomController.GetInstance().CreateRoomType = "0";
            SetTextCancelSelect(toggleArr[(int)ToggleType.ClubRoom].transform.FindChild("Label").GetComponent<Text>());
            pitchClubRoom = false;
            LianMengToggleInit();
        }    
    }
      
    public void LianMengToggleClick()
    {
        if (toggleArr[(int) ToggleType.LianMeng].isOn)
        {
            SetTextSelect(toggleArr[(int) ToggleType.LianMeng].transform.FindChild("Label").GetComponent<Text>());
            CreateRoomController.GetInstance().CreateRoomType = "2";
        }
        else
        {
            SetTextCancelSelect(toggleArr[(int) ToggleType.LianMeng].transform.FindChild("Label").GetComponent<Text>());
            CreateRoomController.GetInstance().CreateRoomType = "1";
        }
    }

    //底分和带入积分
    public void ScoreSliderValueChange()
    {
        int temp = Mathf.FloorToInt(scoreSlider.value/0.166f);
        scoreSlider.value = temp*0.166f;
        int botScore = GetBotScore(temp);
        SetBotScoreTex(botScore);
        SetTakeInScore(botScore*100);
        takeScoreServiceCharge = botScore*20;
        GetAndSetServiceCharge();
        CreateRoomController.GetInstance().BotScore = botScore.ToString();
        CreateRoomController.GetInstance().MinTakeInScore = (botScore*100).ToString();
    }

    public int GetBotScore(double temp)
    {
        int botScore = 0;
        switch (temp.ToString())
        {
            case "0":
                botScore = 2;
                break;
            case "1":
                botScore = 5;
                break;
            case "2":
                botScore = 10;
                break;
            case "3":
                botScore = 25;
                break;
            case "4":
                botScore = 50;
                break;
            case "5":
                botScore = 100;
                break;
            case "6":
                botScore = 200;
                break;
        }
        return botScore;
    }
    public void SetBotScoreTex(int num)
    {
        SetText(textArr[(int)TextType.BotScoreTex], num.ToString());
    }
    public void SetTakeInScore(int num)
    {
        SetText(textArr[(int) TextType.TakeInScore], num.ToString());
    }

    //服务费
    public int takeScoreServiceCharge = 0;
    public int empowerTakeIn = 0;
    public int autoCompute = 0;
    public int vieforZhuang = 0;
    public int oneTimesBet = 0;
    public void GetAndSetServiceCharge()
    {
        if (takeScoreServiceCharge == 0)
        {
            takeScoreServiceCharge = 40;
        }
        int serviceCharge = takeScoreServiceCharge + empowerTakeIn + autoCompute + vieforZhuang + oneTimesBet;       
        SetServiceCharge(serviceCharge);
        CreateRoomController.GetInstance().ServiceCharge = serviceCharge.ToString();
    }
    public void SetServiceCharge(int num)
    {
        SetText(textArr[(int)TextType.ServiceCharge], num.ToString());
    }

    public void SetPropertyTex(int num)
    {
        SetText(textArr[(int)TextType.PropertyTex], num.ToString());
    }

    //设置房间时间
    public void TimeSliderValueChange()
    {
        int temp = Mathf.FloorToInt(timeSlider.value / 0.142f);
        timeSlider.value = temp * 0.142f;
        int time = GetTime(temp);    
        TimeSliderSelect(temp);
        CreateRoomController.GetInstance().RoomExistenceTime = time.ToString();
    }

    private  int GetTime(int temp)
    {
        int time = 0;
        switch (temp.ToString())
        {
            case "0":
                time = 30;
                break;
            case "1":
                time = 60 ;
                break;
            case "2":
                time = 90;
                break;
            case "3":
                time = 120;
                break;
            case "4":
                time = 150;
                break;
            case "5":
                time = 180;
                break;
            case "6":
                time = 240;
                break;
            case "7":
                time = 360;
                break;
        }
        return time;
    }
    private void TimeSliderSelect(int num)
    {
        for (int i = 0; i < 8; i++)
        {
            if (i == num)
            {
                timeSliderGroup.transform.GetChild(i).gameObject.SetActive(true);
            }
            else
            {
                timeSliderGroup.transform.GetChild(i).gameObject.SetActive(false);
            }    
        }

        for (int i = 8; i < 16; i++)
        {
            if (i  == num+8)
            {
                timeSliderGroup.transform.GetChild(i).gameObject.SetActive(false);
            }
            else
            {
                timeSliderGroup.transform.GetChild(i).gameObject.SetActive(true);
            }     
        }
    }

    //创建房间
    private void CreateRoomBtnClick()
    {
        if (isCreatRoom)
        {
            CreateRoomController.GetInstance().ClubOrLianMengID = ClubOrLianMengID;
            CreateRoomController.GetInstance().SendCreatRoomMsg70();
            isCreatRoom = false;
        }
    }

    private bool isCreatRoom = true;
    private float timer;
    private void Update()
    {
        if (!isCreatRoom)
        {
            timer += Time.deltaTime;
            if (timer > 2f)
            {
                timer = 0;
                isCreatRoom = true;
            }
        }
    }
}